package com.example.teststarter.service;

import com.example.teststarter.entity.User;
import com.example.teststarter.mapper.UserMapper;
import com.google.common.collect.ImmutableMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;

/**
 * @author 60055558
 * 项目名称：springboot
 * 类名称：UserService
 * 创建人：60055558
 * 创建时间：2020年7月17日 下午4:09:42
 */
@Service
public class UserService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    private OtherService otherService;

    //查询
    public User Sel(int id) {
        return userMapper.Sel(id);
    }

    //更新
    @Transactional(propagation = Propagation.REQUIRED)
    public boolean updateUserById(User user) {
        userMapper.updateUserById(user);

        otherService.insertUser();
        if (true) {
            throw new RuntimeException("updateUserById 抛异常了");
        }

        return true;
    }

    //新增
    public boolean insertUser() {
    	 /*
    	 //普通for循环插入
    	 for (int i = 0; i <20; i++) {
    	    User user = new User();
    		user.setId(i);
    		user.setUserName("testusername" + i);
    		user.setPassWord("testpassword" + i);
	        user.setRealName("testrealname"+i);
    		userMapper.insertUser(user);
    	  }
    	  */
        //userMapper.insertUser(user);
        List<User> list = new ArrayList<>();
        for (int i = 0; i < 2; i++) {
            User user = new User();
            user.setId(i);
            user.setUserName("*username" + i);
            user.setPassWord("*password*" + i);
            user.setRealName("*realname*" + i);
            list.add(user);
        }
        userMapper.insertUser(list);
        return true;
    }

    //删除
    public boolean deleteById(int id) {
        userMapper.deleteById(id);
        return true;
    }

    /**
     * 传统根据不同条件判断执行不同业务逻辑
     *
     * @param num
     * @return
     */
    public String getUserNameByNum(Integer num) {
        User user = new User();
        user.setUserName("张三丰");
        user.setRealName("三丰丰");
        String userName = "";
        switch (num) {
            case 1:
                userName = String.format("我的名字,name:%s", user.getUserName());
                break;
            case 2:
                userName = String.format("我的名字,name:,realName:%s", user.getUserName(), user.getRealName());
                break;
            default:
                break;
        }
        return userName;
    }

    //定义一个map跟Function组合使用的map
    private final ImmutableMap<Integer, Function<User, String>> PERSON_TYPE_MAP = ImmutableMap.<Integer, Function<User, String>>builder()
            .put(1, person -> String.format("武当山No1,userName:%s", person.getUserName()))
            .put(2, person -> String.format("武当山No1,userName:%s,realName:%s", person.getUserName(), person.getRealName()))
            .build();

    //调用
    public String getFunctionUserNameByNum(Integer num) {
        User user = new User();
        user.setUserName("张三丰");
        user.setRealName("三丰丰");
        return PERSON_TYPE_MAP.get(num).apply(user);
    }


}